import java.util.ArrayList;
import java.util.Collections;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class Fangfa {
    public static void main(String[] args) {
        //1.filter方法
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "张无忌", "周知只", "赵敏", "张良", "谢广坤", "张翠山");
        //filter 过滤 把张开头的留下，其余不要
        list.stream().filter(new Predicate<String>() {
            @Override
            public boolean test(String s) {
                //如果返回值为true，表示当前数据留下
                return s.startsWith("张");
            }
        }).forEach(s -> System.out.println(s));
        //简化
        Stream<String> stream1 = list.stream().filter(s -> s.startsWith("张"));
        //限定三个字
        Stream<String> stream2 = stream1.filter(s -> s.length() == 3);
        stream2.forEach(s -> System.out.println(s));

        //继续简化
        list.stream()
                .filter(s -> s.startsWith("张"))
                .filter(s -> s.length() == 3)
                .forEach(s -> System.out.println(s));

        //2.limit方法,skip方法
        list.stream().limit(3).forEach(s-> System.out.println(s));
        list.stream().skip(4).forEach(s-> System.out.println(s));

        //获取中间元素
        list.stream().skip(3).limit(4).forEach(s-> System.out.println(s));

        //3.distinct,concat
        ArrayList<String> list1=new ArrayList<>();
        Collections.addAll(list,"周允允","洛云姗");
        list.stream().distinct().forEach(s-> System.out.println(s));
        Stream.concat(list.stream(),list1.stream()).forEach(s-> System.out.println(s));
    }
}
